package com.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.business.PurchaseDTO;
import com.boot.feign.ProductFeign;
import com.boot.feign.UserFeign;
import com.boot.product.vo.ProductVo;
import com.boot.result.Result;
import com.boot.storage.pojo.Storage;
import com.boot.service.StorageService;
import com.boot.mapper.StorageMapper;
import com.boot.user.pojo.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author liyan
 * @description 针对表【storage】的数据库操作Service实现
 * @createDate 2025-05-25 16:32:54
 */
@Service
@AllArgsConstructor
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage>
        implements StorageService {

    private StorageMapper storageMapper;
    private ProductFeign productFeign;
    private UserFeign userFeign;

    @Override
    public Result getStorageByProductId(Long id) {
        LambdaQueryWrapper<Storage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Storage::getProductId, id);
        Storage storage = baseMapper.selectOne(queryWrapper);
        if (storage == null) {
            return Result.build(500, "商品库存不存在", null);
        }
        return Result.success(storage.getNum());
    }

    @Override
    public Result deduct(PurchaseDTO purchaseDTO) {
        ObjectMapper objectMapper = new ObjectMapper();
        Long productId = purchaseDTO.getProductId();
        Result result = productFeign.getProductById(productId);
        if (result.getCode() != 200) {
            return Result.build(500, "商品信息有误", null);
        }
        ProductVo product = objectMapper.convertValue(result.getData(), ProductVo.class);
        if (product.getStatus() != 1) {
            return Result.build(500, "商品已下架", null);
        }

        Result storageByProductId = getStorageByProductId(productId);
        if (storageByProductId.getCode() != 200) {
            return Result.build(500, "库存信息有误", null);
        }
        Integer storage = (Integer) storageByProductId.getData();
        if (storage < purchaseDTO.getNum()) {
            return Result.build(500, "库存不足", null);
        }

        Result userById = userFeign.getUserById(purchaseDTO.getUserId());
        if (userById.getCode() != 200) {
            return Result.build(500, "用户信息有误", null);
        }
        User userInfo = objectMapper.convertValue(userById.getData(), User.class);
        if (userInfo.getStatus() != 1) {
            return Result.build(500, "用户被禁用", null);
        }
        BigDecimal totalAmount = product.getPrice().multiply(new BigDecimal(purchaseDTO.getNum()));
        if (userInfo.getBalance().compareTo(totalAmount) < 0) {
            return Result.build(500, "用户余额不足", null);
        }

        LambdaUpdateWrapper<Storage> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Storage::getProductId, productId)
                .set(Storage::getNum, storage - purchaseDTO.getNum());
        int update = storageMapper.update(wrapper);
        if (update == 0) {
            return Result.build(500, "扣减库存失败", null);
        }
        return Result.success(totalAmount);
    }
}




